// Board.cs created with MonoDevelop
// User: kenw at 11:31 AM 5/12/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.Collections.Generic;
using System.Text;

using Gtk;
using Glade;

namespace QDG.Powerball
{
	
	
	public class Board
	{
		
		public List<Entry> entries;
		public CheckButton qp;
		
		public Board(int idx)
		{
			entries = new List<Entry>(6);
			
			for (int i = 0; i < 6; i++)
			{
				Entry e = new Entry("");
				e.MaxLength = 2;
				e.WidthChars = 2;
				e.IsEditable = true;
				e.Alignment = 1.0f;
				entries.Add(e);
			}
			
			qp = new CheckButton("Quick Pick");
			qp.Active = false;
			qp.Clicked += OnQPClicked;
		}
		
		void OnQPClicked(object o, EventArgs ea)
		{
			Console.WriteLine("object {0}  EventArgs: {1}", o, ea);
			
			R250 rnd = new R250((uint)DateTime.Now.Ticks);
			
			int[] results = new int[5];
			
			int curRN = (int)(rnd.Flat() * 55 + 1);
			
			bool dupFound;
			for (int i = 0; i < 5; i++)
			{
				dupFound = true;
				results[i] = 0;
				while (dupFound)
				{
					dupFound = false;
					curRN = (int)(rnd.Flat() * 55 + 1);

					for (int j = 0; j < i; j++)
						if (curRN == results[j])
							dupFound = true;
					
				}
				
				results[i] = curRN;
				entries[i].Text = curRN.ToString();
			}
			entries[5].Text = ((int)(rnd.Flat() * 42 + 1)).ToString();
		}
		
		public int CompareBoard(Board b)
		{
			int results = 0;
			int matches = CheckMatches(b);
			
			bool powerBallMatches = false;
			
			Application.Invoke(delegate{powerBallMatches = b.entries[5].Text == entries[5].Text;});
				
			// we at least have a powerball match
			if (powerBallMatches)
			{
				switch(matches)
				{
				case 0: // powerball only
					//Application.Invoke(delegate{results = UpdatePB();});
					break;
					
				case 1: // powerball + 1
					//Application.Invoke(delegate{results = UpdatePB1();});
					break;
					
				case 2: // powerball + 2
					//Application.Invoke(delegate{results = UpdatePB2();});
					break;
					
				case 3: // powerball + 3
					//Application.Invoke(delegate{results = UpdatePB3();});
					break;
					
				case 4:  // powerball + 4
					//Application.Invoke(delegate{results = UpdatePB4();});
					break;
					
				case 5:  // jackpot!
					//Application.Invoke(delegate{results = UpdateJackpot();});
					break;
				}
			}
			else
			{
				switch(matches)
				{
				case 0: // no matches
				case 1: // 1 match
				case 2: // 2 matches
					break;
					
				case 3: // 3 balls match
					//Application.Invoke(delegate{results = Update3();});
					break;
					
				case 4:  // 4 balls match
					//Application.Invoke(delegate{results = Update4();});
					break;
					
				case 5:  // 5 balls match
					//Application.Invoke(delegate{results = Update5();});
					break;
				}
			}
			
			return results;
		}
		
		private int CheckMatches(Board b)
		{
			int matches = 0;

			Application.Invoke(delegate{
				for (int i = 0; i < 5; i++)
				{
					for (int j = 0; j < 5; j++)
					{
						if (entries[i].Text == b.entries[j].Text)
							matches++;
					}
				}
			});
			return matches;
		}
		
		public override string ToString()
		{
			StringBuilder sb = new StringBuilder("[");
			for (int i = 0; i < 6; i++)
				sb.AppendFormat(" {0}", entries[i].Text);
			sb.Append("]");
			
			return sb.ToString();
		}
	}
}
